<?php
    session_start();
    include('../sql.php');
    $uid = $_SESSION['id'];
    if (isset($_GET['action'])) {
        $action = $_GET['action'];
        if (isset($_SESSION['id'])) {
            switch ($action) {
                case 'loginout':
                    session_destroy();
                    header("Location: /");
                    break;
                case 'studentspass':
                    $id = $_GET['id'];
                    $point = $_GET['point'];
                    $sql = "UPDATE user SET first = $point WHERE id = $id";
                    $result = $link->query($sql);
                    if ($result) {
                        header("Location: " . $_SERVER["HTTP_REFERER"]);
                    } else {
                        echo "失败" . $link->error;
                    }
                    break;
                case 'updatearticle':
                    $id = $_POST['id'];
                    $title = $_POST['title'];
                    $text = $_POST['text'];
                    $sql = "UPDATE article SET title = ?, text = ? WHERE id = ?";
                    $stmt = $link->prepare($sql);
                    $stmt->bind_param("ssi", $title, $text, $id);
                    if ($stmt->execute()) {
                        echo "修改成功";
                    } else {
                        echo "修改失败: " . $link->error;
                    }
                    $stmt->close();
                    break;
                case 'admin':
                    $user = $_POST['user'];
                    $password = md5($_POST['password']);
                    if (empty($user)) {
                        echo "管理员用户名不能为空";
                    } else {
                        if (empty($password)) {
                            echo "管理员密码不能为空";
                        } else {
                            $sql = "INSERT INTO admin_user (id,name,password) VALUES ('','$user','$password')";
                            $result = $link->query($sql);
                            if ($result) {
                                echo "添加成功！";
                            } else {
                                echo "添加失败。" . $link->error;
                            }
                        }
                    }
                    break;
                case 'pass':
                    $password = md5($_POST['password']);
                    if (empty($password)) {
                            echo "管理员密码不能为空";
                        } else {
                            $sql = "UPDATE admin_user SET password = \"$password\" WHERE id = $uid";
                            $result = $link->query($sql);
                            if ($result) {
                                echo "修改成功";
                            } else {
                                echo "修改失败" . $link->error;
                            }
                        }
                    break;
                case 'sdel':
                    $id = $_GET['id'];
                    $sql = "DELETE FROM speciality WHERE id = $id";
                    $result = $link->query($sql);
                    if ($result) {
                        header("Location: " . $_SERVER["HTTP_REFERER"]);
                    } else {
                        echo "删除失败" . $link->error;
                    }
                    break;
                case 'admindel':
                    $id = $_GET['id'];
                    $sql = "DELETE FROM admin_user WHERE id = $id";
                    $result = $link->query($sql);
                    if ($result) {
                        header("Location: " . $_SERVER["HTTP_REFERER"]);
                    } else {
                        echo "删除失败" . $link->error;
                    }
                    break;
                case 'delarticle':
                    $id = $_GET['id'];
                    $sql = "DELETE FROM article WHERE id = $id";
                    $result = $link->query($sql);
                    if ($result) {
                        header("Location: " . $_SERVER["HTTP_REFERER"]);
                    } else {
                        echo "删除失败" . $link->error;
                    }
                    break;
                case 'disable':
                    $why = $_POST['why'];
                    $id = $_GET['id'];
                    $sql = "UPDATE user SET why = \"$why\" WHERE id = $id";
                    $result = $link->query($sql);
                    if ($result) {
                        header("Location: " . $_SERVER["HTTP_REFERER"]);
                    } else {
                        echo "更新失败" . $link->error;
                    }
                    break;
                case 'second':
                    $second = $_POST['second'];
                    $id = $_GET['id'];
                    $sql = "UPDATE user SET second = $second WHERE id = $id";
                    $result = $link->query($sql);
                    if ($result) {
                        header("Location: " . $_SERVER["HTTP_REFERER"]);
                    } else {
                        echo "更新失败" . $link->error;
                    }
                    break;
                case 'speciality':
                    $speciality = $_POST['speciality'];
                    $mlzyl = $_POST['zylb'];
                    $xw = $_POST['xw'];
                    $years = $_POST['years'];
                    $school = $_POST['school'];
                    $total = $_POST['total'];
                    $price = $_POST['price'];
                    $sql = "INSERT INTO speciality (id,name,mlzyl,xwsyml,years,school,total,price) VALUES ('','$speciality','$mlzyl','$xw','$years','$school','$total','$price')";
                    $result = $link->query($sql);
                    if ($result) {
                        echo "添加成功！";
                    } else {
                        echo "添加失败。" . $link->error;
                    }
                    break;
                case 'updatespeciality':
                    $specialityid = $_GET['id'];
                    $speciality = $_POST['speciality'];
                    $mlzyl = $_POST['zylb'];
                    $xw = $_POST['xw'];
                    $years = $_POST['years'];
                    $school = $_POST['school'];
                    $total = $_POST['total'];
                    $price = $_POST['price'];
                    $sql = "UPDATE speciality SET name = \"$speciality\", mlzyl = \"$mlzyl\", xwsyml = \"$xw\", years = \"$years\", school = \"$school\", total = \"$total\", price =\"$price\" WHERE id = $specialityid";
                    $result = $link->query($sql);
                    if ($result) {
                        echo "修改成功！";
                    } else {
                        echo "修改失败。" . $link->error;
                    }
                    break;
                case 'article':
                    $time = time();
                    $ip = $_SERVER["REMOTE_ADDR"];
                    $title = $_POST['title'];
                    $text = $_POST['text'];
                    $sql = "INSERT INTO article (id,title,text,time) VALUES ('','$title','$text','$time')";
                    $result = $link->query($sql);
                    if ($result) {
                        echo "发表成功！";
                    } else {
                        echo "发表失败。" . $link->error;
                    }
                    break;
                case 'webinfo':
                    $webname = $_POST['webname'];
                    $address = $_POST['address'];
                    $mail = $_POST['mail'];
                    $tel = $_POST['tel'];
                    $icp = $_POST['icp'];
                    $banner = $_POST['banner'];
                    $introduce = $_POST['introduce'];
                    $checkSql = "SELECT id FROM webinfo WHERE id = 1";
                    $checkResult = $link->query($checkSql);
                    if ($checkResult->num_rows > 0) {
                        $sql = "UPDATE webinfo SET webname = \"$webname\", mail = \"$mail\", tel = \"$tel\", address = \"$address\", icp = \"$icp\", banner = \"$banner\", introduce =\"$introduce\" WHERE id = 1";
                    } else {
                        $sql = "INSERT INTO webinfo (id, webname, mail, tel, address, icp, banner, introduce) VALUES (1, \"$webname\", \"$mail\", \"$tel\", \"$address\", \"$icp\", \"$banner\", \"$introduce\")";
                    }
                    $result = $link->query($sql);
                    if ($result) {
                        echo "更新成功";
                    } else {
                        echo "更新失败" . $link->error;
                    }
                    break;
                case 'reg':
                    $int = $_GET['int'];
                    $sql = "UPDATE webinfo SET reg = $int WHERE id = 1";
                    $result = $link->query($sql);
                    if ($result) {
                        header("Location: " . $_SERVER["HTTP_REFERER"]);
                    } else {
                        echo "更新失败" . $link->error;
                    }
                    break;
            }
        } else { ?>
    <script type="text/javascript">
        alert("未登入！");
        window.history.back();
    </script>
    <?php }
        } else { ?>
        <script type="text/javascript">
            alert("请正确使用action案例！");
            window.history.back();
        </script>
        <?php } ?>